package com.ffcs.cn.action.base.tree;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.TreeComboBox;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.ffcs.cn.DevelopProperties;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;

public class DialogTreeComboBox extends SmDialog {
	private JTree tree;
	private JLabel labelTreeComboBox;
	private TreeComboBox treeComboBox;

	public DialogTreeComboBox() {
		//设置标题
		setTitle("树ComboBox示例");
		//设置初始化窗口大小
		Dimension dimension = new Dimension(300, 400);
		setSize(dimension);
		//设置最小窗口
		setMinimumSize(dimension);
		//初始化使用控件
		initComponents();
		//初始化布局，并添加控件
		initLayout();
	}

	private void initComponents() {
		//创建节点
		DefaultMutableTreeNode workspaceNode = new DefaultMutableTreeNode("工作空间");

		DefaultMutableTreeNode datasourceNode = new DefaultMutableTreeNode("数据源");
		//添加子节点
		datasourceNode.add(new DefaultMutableTreeNode("DataSource1"));
		datasourceNode.add(new DefaultMutableTreeNode("DataSource2"));
		datasourceNode.add(new DefaultMutableTreeNode("DataSource3"));

		DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode("地图");
		mapNode.add(new DefaultMutableTreeNode("Map1"));
		mapNode.add(new DefaultMutableTreeNode("Map2"));
		mapNode.add(new DefaultMutableTreeNode("Map3"));

		DefaultMutableTreeNode layoutNode = new DefaultMutableTreeNode("布局");
		layoutNode.add(new DefaultMutableTreeNode("Layout1"));
		DefaultMutableTreeNode defaultMutableTreeNodeLayout2 = new DefaultMutableTreeNode("Layout2");
		layoutNode.add(defaultMutableTreeNodeLayout2);
		layoutNode.add(new DefaultMutableTreeNode("Layout3"));

		workspaceNode.add(datasourceNode);
		workspaceNode.add(mapNode);
		workspaceNode.add(layoutNode);

		//创建树
		this.tree = new JTree(workspaceNode);
		//给根节点显示折叠按钮
		this.tree.setShowsRootHandles(true);
		this.labelTreeComboBox = new JLabel(DevelopProperties.getString("String_Label_TreeComboBox"));
		// 创建树ComboBox，并设置树
		this.treeComboBox = new TreeComboBox(this.tree);
		// 设置树ComboBox默认选中节点
		this.treeComboBox.setSelectedItem(new TreePath(defaultMutableTreeNodeLayout2.getPath()));
	}

	private void initLayout(){
		//设置布局为 GridBagLayout 布局
		this.setLayout(new GridBagLayout());

		this.add(this.labelTreeComboBox, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0));
		this.add(this.treeComboBox, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setWeight(1, 0).setFill(GridBagConstraints.HORIZONTAL));

		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 2, 1).setInsets(0, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
	}

}

